<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" width="90%">
    <ContentWrap>
      <el-table v-loading="loading" :data="list" border>
        
        <el-table-column label="排班日期" align="center" prop="date" :formatter="dateFormatter2" width="120"/>
        
        <el-table-column label="上午排班" align="center">
          <el-table-column prop="morningStart" label="开始时间" :formatter="timeArrayTableFormatter" width="120"/>
          <el-table-column prop="morningEnd" label="结束时间" :formatter="timeArrayTableFormatter" width="120"/>
          <el-table-column prop="morningCapacity" label="可预约数" width="100"/>
          <el-table-column prop="morningCount" label="已预约数" width="100"/>
        </el-table-column>
        
        <el-table-column label="下午排班" align="center">
          <el-table-column prop="afternoonStart" label="开始时间" :formatter="timeArrayTableFormatter" width="120"/>
          <el-table-column prop="afternoonEnd" label="结束时间" :formatter="timeArrayTableFormatter" width="120"/>
          <el-table-column prop="afternoonCapacity" label="可预约数" width="100"/>
          <el-table-column prop="afternoonCount" label="已预约数" width="100"/>
        </el-table-column>
        
        <el-table-column label="操作" align="center" width="180" fixed="right">
          <template #default="{row}">
            <el-button 
              type="primary" 
              size="small" 
              @click="goToRegister(row, 1)"
              
            >
              前往挂号界面
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </ContentWrap>
  </Dialog>
</template>

<script lang="ts" setup>
import * as DoctorApi from '@/api/medical/doctor'
import * as ScheduleApi from '@/api/medical/schedule'
import {  dateFormatter2, timeArrayTableFormatter } from '@/utils/formatTime'

defineOptions({ name: 'MedicalDoctorScheduleForm' })

const list = ref<ScheduleApi.ScheduleVO[]>([]) // 列表数据
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const loading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const today = ref(new Date().toISOString().split('T')[0])  // 今天的日期

const router = useRouter()

const goToRegister = async(row: any, period: number) => {
  const doctor = await DoctorApi.getDoctor(row.doctorId)
  router.push({
    path: '/medical/user/userRegister',
    query: {
      doctorId: row.doctorId,
      date: row.date,
      period: period,
      deptId: doctor.deptId,
    }
  })
  dialogVisible.value = false
}


/** 打开弹窗 */
const open = async (doctorId?: number) => {
  dialogVisible.value = true
  dialogTitle.value = '医师排班列表'
if (typeof doctorId === 'number') {
  await fetchScheduleList(doctorId)
} else {
  // 当 id 为 undefined 时，可以添加相应的错误处理逻辑，这里简单打印日志
  console.error('id 为 undefined，无法获取排班列表')
}

}

defineExpose({ open }) // 提供 open 方法，用于打开弹窗

// 添加获取排班列表方法
const fetchScheduleList = async (doctorId: number) => {
  loading.value = true
  try {
    // 将 Date 类型的 today.value 转换为字符串类型
    list.value = await ScheduleApi.getDoctorScheduleList(doctorId, today.value)
  } finally {
    loading.value = false
  }
}

</script>
